package com.bw.module_service.ui

import android.os.Bundle
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.databinding.DataBindingUtil.setContentView
import com.blankj.utilcode.util.ToastUtils
import com.bw.lib_common.base.BaseActivity
import com.bw.lib_storage.mmkv.AuthMMKV
import com.bw.module_service.R
import com.bw.module_service.databinding.ActivityAddVipBinding
import com.bw.module_service.viewmodel.ServiceViewModel
import com.google.gson.Gson
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
@AndroidEntryPoint
class AddVipActivity : BaseActivity<ServiceViewModel,ActivityAddVipBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_add_vip
    private val mViewModel: ServiceViewModel by viewModels()

    override fun initView() {
        mBinding.add.setOnClickListener {
            if (mBinding.etName.text.toString().isEmpty()||mBinding.etDate.text.toString().isEmpty()||mBinding.etTime.text.toString().isEmpty()||mBinding.etDollar.text.toString().isEmpty()||mBinding.vipLevel.text.toString().isEmpty()){
                Toast.makeText(this,"请填写完整信息", Toast.LENGTH_SHORT).show()
            }else{
                val map= mutableMapOf<String,Any>()
                map["userId"]=mBinding.etID.text.toString().toIntOrNull()?:AuthMMKV.userid.toString()
                map["username"]=mBinding.etName.text.toString()
                map["registrationDate"]=mBinding.etDate.text.toString()
                map["duration"]=mBinding.etTime.text.toString().toIntOrNull()?:0
                map["registrationFee"]=mBinding.etDollar.text.toString().toIntOrNull() ?:0
                map["membershipLevel"]=mBinding.vipLevel.text.toString()
                map["isPaid"]=true
                val json= Gson().toJson(map)
                val body= RequestBody.create("application/json".toMediaTypeOrNull(),json)
                mViewModel.addVip(body)
                finish()
            }
        }
        mBinding.back.setOnClickListener {
            finish()
        }
    }

    override fun initListener() {
        mViewModel.addVipLiveData_.observe(this){
            finish()
        }
    }

}